package cn.lcfms.app.admin.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import cn.lcfms.app.BaseController;
import cn.lcfms.bin.BaseCache;
import cn.lcfms.bin.Broadcast;
import cn.lcfms.bin.Userinfo;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.utils.CommonUtils;
import net.sf.json.JSONObject;
import cn.lcfms.bin.view.Common;

public class AdminBaseController extends BaseController{	
	/**
	 * 该方法将在执行控制器之前被执行
	 * @throws Exception 
	 */
    @Override
    protected void beforeProtected(HttpServletRequest request,HttpServletResponse response){
    	super.beforeProtected(request,response);
    	String basePath=(String) request.getAttribute("APP");
    	request.setAttribute("JS", basePath+"statics/tag/js/"); 
    	request.setAttribute("CSS", basePath+"statics/tag/css/"); 
    	request.setAttribute("IMG", basePath+"statics/tag/images/");   
    	if(BaseCache.ITEMCACHE.size()==0){
    		set_item_cache();
    	}
    }  
    /**
   	 * 该方法将在执行控制器之后，调入模板之前被执行
     * @throws IOException 
   	 */
    @Override
    protected void afterProtected(HttpServletRequest request,HttpServletResponse response,ModelAndView view) throws IOException{  	
    	super.afterProtected(request,response,view);    
		setBroadcastId(request);
    	if(view!=null && view.getViewName()!=null){	
    		String viewname=view.getViewName();	
    		if(viewname.startsWith("local:")){
    			view.setViewName("admin/local");
    			view.addObject("LOCAL", viewname.substring(6));	
    		}
    		if(null!=view.getModel().get("CommonPage")) {
    			CommonPage(request,view);
    		}					
		}
    }
	
	private void CommonPage(HttpServletRequest request, ModelAndView view) {
		Map<String, Object> model = view.getModel();
		Common common=(Common) model.get("CommonPage");
		common.setPage(request);
	}
	
	//初始化所有栏目
	protected void set_item_cache(){
		BaseService service = Application.getService("item");
		List<HashMap<String, Object>> list=service.order("sort").selectList();
		for(int i=0;i<list.size();i++){
			int itemId=(int) list.get(i).get("itemId");
			for(int m=0;m<list.size();m++){
				int parentId=(int) list.get(m).get("parentId");
				if(parentId==itemId){
					list.get(i).put("hasChild", true);
					break;
				}
			}
		}		
		BaseCache.ITEMCACHE=list;	
	}
	
    //设置用户通知id
    private void setBroadcastId(HttpServletRequest request){		
		int broadid = Application.getBroadcastId();
		if(null==Userinfo.get("aid")) {
			return;
		}
		int userId=(int) Userinfo.get("aid");
		String userName=(String) Userinfo.get("aname");
		Userinfo.put("broadid", CommonUtils.md5(String.valueOf(userId)+userName+broadid));	
	}
    
    protected void success(String content){
    	JSONObject object=new JSONObject();
    	object.put("content", content);
    	object.put("style", "alert-success");
    	object.put("title", "提示");
    	object.put("position", "topRight");
    	object.put("autoClose", true);
    	try {
			Broadcast.echo((int)Userinfo.get("aid"), object.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
    protected void error(String content){
    	JSONObject object=new JSONObject();
    	object.put("content", content);
    	object.put("style", "alert-danger");
    	object.put("title", "提示");
    	object.put("position", "topRight");
    	object.put("autoClose", true);
    	try {
			Broadcast.echo((int)Userinfo.get("aid"), object.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
